package com.guigu.community.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.Quote;
import com.guigu.community.service.QuoteService;
import com.guigu.community.vo.QuoteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/quotes")
public class QuoteController {

    @Autowired
    private QuoteService quoteService;

    @GetMapping("/suppliers/{supplierId}")
    public List<Quote> getQuotesBySupplierId(@PathVariable Integer supplierId) {
        return quoteService.getQuotesBySupplierId(supplierId);
    }

    @PostMapping("add")
    public String add(@RequestBody Quote quote){
        quote.setIsSellStatus(0);//默认是供应状态的
        boolean save = quoteService.save(quote);
        if (save)
            return "yes";
        return "no";
    }

    @PostMapping("selectByVo/{productId}")
    public List<Quote> selectByVo(@RequestBody QuoteVo vo) {
        return quoteService.selectByVo(vo);
    }


    @PostMapping("queryByProductUserInfo/{current}/{pageSize}/{uid}")
    public PageInfo<Quote> queryByProductUserInfo(@PathVariable Integer current,
                                                  @PathVariable Integer pageSize,
                                                  @PathVariable Integer uid,
                                                  @RequestBody QuoteVo vo){
        vo.setSearchUserId(String.valueOf(uid));
        return quoteService.queryQuoteUserProductInfo(current, pageSize, vo);
    }


    @PutMapping("modifQutoe")
    public String modifQutoe(@RequestBody Quote quote){
        boolean b = quoteService.updateById(quote);
        if (b) {
            return "Yes";
        }
        return "No";
    }

    @PostMapping("deleteQuote/{qid}")
    public String deleteQuote(@PathVariable Integer qid){
        boolean b = quoteService.removeById(qid);
        if (b) {
            return "Yes";
        }
        return "No";
    }

    // 根据商品 ID 查询报价信息
    @PostMapping("/getSupplierByProductId/{productId}")
    public List<Quote> getSupplierByProductId(@PathVariable("productId") Integer productId) {
        System.out.println("Received productId: " + productId);
        return quoteService.getQuotesByProductId(productId);
    }
}